<?php
class AutoLoader {
	static $list_file = '';
	static function loadClass($class) {
		if (class_exists($class, false) || interface_exists($class, false)) {
			return;
		}
		// auto discover the path from the class name
		
		$file = ROOT_PATH . "core/{$class}.php";
		
		if( file_exists($file) ){
			 require_once $file;
			 
			 if(DEBUG){
			 	self::$list_file .= "<li>$file</li>";
			 }
		}
		else{
			$file = ROOT_PATH . "includes/{$class}.php";//$file = ROOT_PATH . "core/" . str_replace("_", DIRECTORY_SEPARATOR, $class) . ".php";
			
			if( file_exists($file) ){
			    require_once $file;
			    
				if(DEBUG){
					self::$list_file .= "<li>$file</li>";
				}
			}
		}
	}

	static function autoload($class){
		try{
		    @self::loadClass($class);
		    return $class;
		}
		catch (Exception $e){
		    return false;
		}
	}

	static function registerAutoload($class = 'AutoLoader', $enabled = true) {
		if (!function_exists('spl_autoload_register')) {
			throw new Exception('spl_autoload does not exist in this PHP installation');
		}

		if ($enabled === true){
			spl_autoload_register(array($class, 'autoload'));
		}
		else
		{
			spl_autoload_unregister(array($class, 'autoload'));
		}
	}
}

AutoLoader::registerAutoload();
?>